#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2021/6/25 下午4:18
# @Author  : HUANG Xiong
# @Site    : 
# @File    : my_oracle.py
# @Software: PyCharm 

"""
脚本说明：创建oracle数据库连接
"""
from sqlalchemy.engine import create_engine
from quant_researcher.quant.project_tool.conf import LOCAL_CONF


def get_oracle_conn(**kwargs):
    """
    构造一个数据库连接对象

    :param kwargs:
        - host，str，主机IP，默认：从本地配置文件中读取
        - port，int，端口，默认：从本地配置文件中读取
        - sid，str，唯一标识，默认：从本地配置文件中读取
        - user，str，用户名，默认：从本地配置文件中读取
        - password，str，密码，默认：从本地配置文件中读取
    :return: 连接对象
    """
    host = kwargs.pop('host', LOCAL_CONF['oracle']['ip'])
    port = kwargs.pop('port', LOCAL_CONF.getint('oracle', 'port'))
    service = kwargs.pop('service', LOCAL_CONF['oracle']['service'])
    user = kwargs.pop('user', LOCAL_CONF['oracle']['user'])
    password = kwargs.pop('password', LOCAL_CONF['oracle']['password'])

    dialect = 'oracle'
    sql_driver = 'cx_oracle'
    engine_path_win_auth = dialect + '+' + sql_driver + '://' + user + ':' + password + '@' + host + ':' + str(
        port) + '/?service_name=' + service

    conn = create_engine(engine_path_win_auth)

    return conn


if __name__ == '__main__':
    aaa = get_oracle_conn()
